#include "test.h"
#include <stdio.h>

extern unsigned char frame_number;
//extern char frame_number;

int hex2string(unsigned char*src,int src_len,unsigned char*dst,int dst_len)
{
	static unsigned char hex[]="0123456789abcdef";
	if(dst_len<2*src_len)
	{
		return -1;
	}
	while(src_len--)
	{
      *dst++ = hex[*src >> 4];
      *dst++ = hex[*src++&0xf]; 
	}
	//dst[(src_len+1)*2]='\0';
    dst[src_len*2+2]='\r';
	dst[src_len*2+3]='\n';
	dst[src_len*2+4]='\0';
	//dst[(src_len*2+1)]='\r';
	//dst[(src_len+2)*2]='\n';
/*
    printf("src=%s\n",src);
    printf("src[0]=%02x\n",src[0]); 

    printf("dst=%s\n",dst); 
    printf("dst[0]=%d\n",dst[0]); 

    printf("dst[1]=%02x\n",dst[1]); 
    printf("dst[12]=%02x\n",dst[dst_len-1]); 
*/
    return 0;
}

int string2hex(unsigned char*src,int src_len,unsigned char*dst)
{
	int i=0;
	for(i=0;i<src_len;i++)
               {
                dst[i]=TwoCharToByte(src[2*i],src[2*i+1]);
                //printf("recv=%x\n",dst[i] );
               }
    return 0;
}

unsigned char* checkhexsum(unsigned char*src,int src_len)
{
	int i=0;
	unsigned char sum=0;
	for(i=0;i<src_len-1;i++)
               {
                sum=sum+src[i];
               }
    if(sum==src[src_len-1])
    {
    	return src;
    }
    else
    {
    	return 0;
    }
}

int IsHexChar(char ch)
{
	if((ch>='0' && ch<='9') || (ch>='a' && ch<='f') ||(ch>='A' && ch<='F'))
		return 1;
	else
		return 0;
}

unsigned char CharToValue(char c)
{
	if(c>='0' && c<='9')
	{
		return c-'0';
	}
	if(c>='A' && c<='F')
	{
		return c-'A'+10;
	}
	if(c>='a' && c<='f')
	{
		return c-'a'+10;
	}
}

unsigned char TwoCharToByte(char h,char l)
{
    return (unsigned char)(CharToValue(h)*16 + CharToValue(l));
}


unsigned char* dev_login(char*prodkey,char*devtid,int fd)
{
	unsigned char dst[138+3];
	unsigned char valid_data[69];
	int i=0;
    //extern unsigned char frame_number;

	frame_number++;
	valid_data[0]=0x48;
	valid_data[1]=0x45;
	valid_data[2]=0x01;
    valid_data[3]=frame_number;
	//valid_data[3]=0x00;
	for(i=0;i<32;i++)
	{
		valid_data[i+4]=prodkey[i];
		valid_data[i+36]=devtid[i];
	}

	sum_valid(valid_data);

	hex2string(valid_data,69,dst,138);
	uartdate_send(fd,dst);
    return dst;
}

unsigned char* auth_recog(char*decrypt,int fd)
{
	unsigned char dst[42+3];
	unsigned char valid_data[21];
	int i=0;

	frame_number++;
	valid_data[0]=0x48;
	valid_data[1]=0x15;
	valid_data[2]=0x03;
	valid_data[3]=frame_number;
	//valid_data[3]=0x00;
	for(i=0;i<16;i++)
	{
		valid_data[i+4]=decrypt[i];
	}

	sum_valid(valid_data);

	hex2string(valid_data,21,dst,42);
	uartdate_send(fd,dst);
    return dst;
}

unsigned char* heart_beat(int fd)
{
	unsigned char dst[10+3];
	unsigned char valid_data[5];
    //extern unsigned char frame_number;

	frame_number++;
	valid_data[0]=0x48;
	valid_data[1]=0x05;
	valid_data[2]=0x0b;
    valid_data[3]=frame_number;
	//valid_data[3]=0x00;

	sum_valid(valid_data);

	hex2string(valid_data,5,dst,10);
	uartdate_send(fd,dst);
    return dst;
}

unsigned char* report_state(int fd)
{
	unsigned char dst[188+3];
	unsigned char valid_data[94];
	int i=0;
    //extern unsigned char frame_number;

	frame_number++;
	valid_data[0]=0x48;
	valid_data[1]=0x5e;
	valid_data[2]=0x05;
    valid_data[3]=frame_number;
	//valid_data[3]=0x00;

	for(i=0;i<89;i++)
	{
		valid_data[i+4]=0x00;
	}

	sum_valid(valid_data);

	hex2string(valid_data,94,dst,188);
	uartdate_send(fd,dst);
    return dst;
}


void sum_valid(char *valid)
{
		int i=0;
		unsigned char sum=0;

	    for(i=0;i<(valid[1]-1);i++)
    {
        sum=sum+valid[i];
    }

   		 valid[valid[1]-1]=sum;

}


